// MIT License

// Copyright (c) 2019 Erin Catto

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "box2d/b2_body.h"
#include "box2d/b2_pulley_joint.h"
#include "box2d/b2_time_step.h"

// Pulley:
// length1 = norm(p1 - s1)
// length2 = norm(p2 - s2)
// C0 = (length1 + ratio * length2)_initial
// C = C0 - (length1 + ratio * length2)
// u1 = (p1 - s1) / norm(p1 - s1)
// u2 = (p2 - s2) / norm(p2 - s2)
// Cdot = -dot(u1, v1 + cross(w1, r1)) - ratio * dot(u2, v2 + cross(w2, r2))
// J = -[u1 cross(r1, u1) ratio * u2  ratio * cross(r2, u2)]
// K = J * invM * JT
//   = invMass1 + invI1 * cross(r1, u1)^2 + ratio^2 * (invMass2 + invI2 * cross(r2, u2)^2)

void b2PulleyJointDef::Initialize(b2Body* bA, b2Body* bB,
        const b2Vec2& groundA, const b2Vec2& groundB,
        const b2Vec2& anchorA, const b2Vec2& anchorB,
        float r)
{
  bodyA = bA;
  bodyB = bB;
  groundAnchorA = groundA;
  groundAnchorB = groundB;
  localAnchorA = bodyA->GetLocalPoint(anchorA);
  localAnchorB = bodyB->GetLocalPoint(anchorB);
  b2Vec2 dA = anchorA - groundA;
  lengthA = dA.Length();
  b2Vec2 dB = anchorB - groundB;
  lengthB = dB.Length();
  ratio = r;
  b2Assert(ratio > b2_epsilon);
}

b2PulleyJoint::b2PulleyJoint(const b2PulleyJointDef* def)
: b2Joint(def)
{
  m_groundAnchorA = def->groundAnchorA;
  m_groundAnchorB = def->groundAnchorB;
  m_localAnchorA = def->localAnchorA;
  m_localAnchorB = def->localAnchorB;

  m_lengthA = def->lengthA;
  m_lengthB = def->lengthB;

  b2Assert(def->ratio != 0.0f);
  m_ratio = def->ratio;

  m_constant = def->lengthA + m_ratio * def->lengthB;

  m_impulse = 0.0f;
}

void b2PulleyJoint::InitVelocityConstraints(const b2SolverData& data)
{
  m_indexA = m_bodyA->m_islandIndex;
  m_indexB = m_bodyB->m_islandIndex;
  m_localCenterA = m_bodyA->m_sweep.localCenter;
  m_localCenterB = m_bodyB->m_sweep.localCenter;
  m_invMassA = m_bodyA->m_invMass;
  m_invMassB = m_bodyB->m_invMass;
  m_invIA = m_bodyA->m_invI;
  m_invIB = m_bodyB->m_invI;

  b2Vec2 cA = data.positions[m_indexA].c;
  float aA = data.positions[m_indexA].a;
  b2Vec2 vA = data.velocities[m_indexA].v;
  float wA = data.velocities[m_indexA].w;

  b2Vec2 cB = data.positions[m_indexB].c;
  float aB = data.positions[m_indexB].a;
  b2Vec2 vB = data.velocities[m_indexB].v;
  float wB = data.velocities[m_indexB].w;

  b2Rot qA(aA), qB(aB);

  m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);

  // Get the pulley axes.
  m_uA = cA + m_rA - m_groundAnchorA;
  m_uB = cB + m_rB - m_groundAnchorB;

  float lengthA = m_uA.Length();
  float lengthB = m_uB.Length();

  if (lengthA > 10.0f * b2_linearSlop)
  {
    m_uA *= 1.0f / lengthA;
  }
  else
  {
    m_uA.SetZero();
  }

  if (lengthB > 10.0f * b2_linearSlop)
  {
    m_uB *= 1.0f / lengthB;
  }
  else
  {
    m_uB.SetZero();
  }

  // Compute effective mass.
  float ruA = b2Cross(m_rA, m_uA);
  float ruB = b2Cross(m_rB, m_uB);

  float mA = m_invMassA + m_invIA * ruA * ruA;
  float mB = m_invMassB + m_invIB * ruB * ruB;

  m_mass = mA + m_ratio * m_ratio * mB;

  if (m_mass > 0.0f)
  {
    m_mass = 1.0f / m_mass;
  }

  if (data.step.warmStarting)
  {
    // Scale impulses to support variable time steps.
    m_impulse *= data.step.dtRatio;

    // Warm starting.
    b2Vec2 PA = -(m_impulse) * m_uA;
    b2Vec2 PB = (-m_ratio * m_impulse) * m_uB;

    vA += m_invMassA * PA;
    wA += m_invIA * b2Cross(m_rA, PA);
    vB += m_invMassB * PB;
    wB += m_invIB * b2Cross(m_rB, PB);
  }
  else
  {
    m_impulse = 0.0f;
  }

  data.velocities[m_indexA].v = vA;
  data.velocities[m_indexA].w = wA;
  data.velocities[m_indexB].v = vB;
  data.velocities[m_indexB].w = wB;
}

void b2PulleyJoint::SolveVelocityConstraints(const b2SolverData& data)
{
  b2Vec2 vA = data.velocities[m_indexA].v;
  float wA = data.velocities[m_indexA].w;
  b2Vec2 vB = data.velocities[m_indexB].v;
  float wB = data.velocities[m_indexB].w;

  b2Vec2 vpA = vA + b2Cross(wA, m_rA);
  b2Vec2 vpB = vB + b2Cross(wB, m_rB);

  float Cdot = -b2Dot(m_uA, vpA) - m_ratio * b2Dot(m_uB, vpB);
  float impulse = -m_mass * Cdot;
  m_impulse += impulse;

  b2Vec2 PA = -impulse * m_uA;
  b2Vec2 PB = -m_ratio * impulse * m_uB;
  vA += m_invMassA * PA;
  wA += m_invIA * b2Cross(m_rA, PA);
  vB += m_invMassB * PB;
  wB += m_invIB * b2Cross(m_rB, PB);

  data.velocities[m_indexA].v = vA;
  data.velocities[m_indexA].w = wA;
  data.velocities[m_indexB].v = vB;
  data.velocities[m_indexB].w = wB;
}

bool b2PulleyJoint::SolvePositionConstraints(const b2SolverData& data)
{
  b2Vec2 cA = data.positions[m_indexA].c;
  float aA = data.positions[m_indexA].a;
  b2Vec2 cB = data.positions[m_indexB].c;
  float aB = data.positions[m_indexB].a;

  b2Rot qA(aA), qB(aB);

  b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
  b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);

  // Get the pulley axes.
  b2Vec2 uA = cA + rA - m_groundAnchorA;
  b2Vec2 uB = cB + rB - m_groundAnchorB;

  float lengthA = uA.Length();
  float lengthB = uB.Length();

  if (lengthA > 10.0f * b2_linearSlop)
  {
    uA *= 1.0f / lengthA;
  }
  else
  {
    uA.SetZero();
  }

  if (lengthB > 10.0f * b2_linearSlop)
  {
    uB *= 1.0f / lengthB;
  }
  else
  {
    uB.SetZero();
  }

  // Compute effective mass.
  float ruA = b2Cross(rA, uA);
  float ruB = b2Cross(rB, uB);

  float mA = m_invMassA + m_invIA * ruA * ruA;
  float mB = m_invMassB + m_invIB * ruB * ruB;

  float mass = mA + m_ratio * m_ratio * mB;

  if (mass > 0.0f)
  {
    mass = 1.0f / mass;
  }

  float C = m_constant - lengthA - m_ratio * lengthB;
  float linearError = b2Abs(C);

  float impulse = -mass * C;

  b2Vec2 PA = -impulse * uA;
  b2Vec2 PB = -m_ratio * impulse * uB;

  cA += m_invMassA * PA;
  aA += m_invIA * b2Cross(rA, PA);
  cB += m_invMassB * PB;
  aB += m_invIB * b2Cross(rB, PB);

  data.positions[m_indexA].c = cA;
  data.positions[m_indexA].a = aA;
  data.positions[m_indexB].c = cB;
  data.positions[m_indexB].a = aB;

  return linearError < b2_linearSlop;
}

b2Vec2 b2PulleyJoint::GetAnchorA() const
{
  return m_bodyA->GetWorldPoint(m_localAnchorA);
}

b2Vec2 b2PulleyJoint::GetAnchorB() const
{
  return m_bodyB->GetWorldPoint(m_localAnchorB);
}

b2Vec2 b2PulleyJoint::GetReactionForce(float inv_dt) const
{
  b2Vec2 P = m_impulse * m_uB;
  return inv_dt * P;
}

float b2PulleyJoint::GetReactionTorque(float inv_dt) const
{
  B2_NOT_USED(inv_dt);
  return 0.0f;
}

b2Vec2 b2PulleyJoint::GetGroundAnchorA() const
{
  return m_groundAnchorA;
}

b2Vec2 b2PulleyJoint::GetGroundAnchorB() const
{
  return m_groundAnchorB;
}

float b2PulleyJoint::GetLengthA() const
{
  return m_lengthA;
}

float b2PulleyJoint::GetLengthB() const
{
  return m_lengthB;
}

float b2PulleyJoint::GetRatio() const
{
  return m_ratio;
}

float b2PulleyJoint::GetCurrentLengthA() const
{
  b2Vec2 p = m_bodyA->GetWorldPoint(m_localAnchorA);
  b2Vec2 s = m_groundAnchorA;
  b2Vec2 d = p - s;
  return d.Length();
}

float b2PulleyJoint::GetCurrentLengthB() const
{
  b2Vec2 p = m_bodyB->GetWorldPoint(m_localAnchorB);
  b2Vec2 s = m_groundAnchorB;
  b2Vec2 d = p - s;
  return d.Length();
}

void b2PulleyJoint::Dump()
{
  int32 indexA = m_bodyA->m_islandIndex;
  int32 indexB = m_bodyB->m_islandIndex;

  b2Dump("  b2PulleyJointDef jd;\n");
  b2Dump("  jd.bodyA = bodies[%d];\n", indexA);
  b2Dump("  jd.bodyB = bodies[%d];\n", indexB);
  b2Dump("  jd.collideConnected = bool(%d);\n", m_collideConnected);
  b2Dump("  jd.groundAnchorA.Set(%.9g, %.9g);\n", m_groundAnchorA.x, m_groundAnchorA.y);
  b2Dump("  jd.groundAnchorB.Set(%.9g, %.9g);\n", m_groundAnchorB.x, m_groundAnchorB.y);
  b2Dump("  jd.localAnchorA.Set(%.9g, %.9g);\n", m_localAnchorA.x, m_localAnchorA.y);
  b2Dump("  jd.localAnchorB.Set(%.9g, %.9g);\n", m_localAnchorB.x, m_localAnchorB.y);
  b2Dump("  jd.lengthA = %.9g;\n", m_lengthA);
  b2Dump("  jd.lengthB = %.9g;\n", m_lengthB);
  b2Dump("  jd.ratio = %.9g;\n", m_ratio);
  b2Dump("  joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
}

void b2PulleyJoint::ShiftOrigin(const b2Vec2& newOrigin)
{
  m_groundAnchorA -= newOrigin;
  m_groundAnchorB -= newOrigin;
}
